include([
         'renderers.All'
         , 'com.unknown.GameWindow'
         , 'PathBuilder'], 
function(){
	
	declare('PathPainter')
	
	PathPainter = function(config){
		if( config ){
			this.pathId  = config.pathId  || '';
			this._builder = config.builder || new PathBuilder();
			this._baseUrl = config.baseUrl || '';
			this.fill     = config.fill === false ? false : true;
			this.fillOnTop = config.fillOnTop ? true : false;
			this.border   = config.border === false ? false : true;
		}else{
			this.pathId   = '';
			this._baseUrl = '';
			this.fill   = true;
			this.fillOnTop = false;
			this.border = true;
			this._builder = new PathBuilder();
		}
	};
	
	PathPainter.prototype = {
		paint : function(cell){
			this._clean(cell);
			if( this.fill && !this.fillOnTop )
				this._fill(cell);
			if( this.border )
				this._border(cell);
			if( this.fill && this.fillOnTop )
				this._fill(cell);
		},
		erase : function(cell){
			iterate(cell.images, function(i){i.remove();})
			//cell.path = null;
			//this._clean(cell);
			//this._rebuildFill( this._getCellsWithPaths(cell) );
			//this._rebuildBorders( this._getCellsWithPaths(cell) );
		},
		_createPathImage : function(src, cell, zindex){
			var contents = $(document.createElement('img'));
			contents.attr('src', this._baseUrl + src+'.png');
			contents.css('position', 'absolute');
			contents.css('top', (cell.row)*15+cell.owner._offsetY);
			contents.css('left', (cell.col)*35+cell.owner._offsetX);
			contents.css('z-index', 500);
			cell.owner.container.append(contents);
			//$('#floor').append(contents);
			if(!cell.images) cell.images = [];
			cell.images.push( contents );
		},		
		_fill : function(cell){
			var hasPath = this._getCellsWithPaths(cell);
			var pathId = this.pathId;
			if( hasPath.length ){
				cell.path = {id:pathId, solo:false};
				hasPath.push( cell );								
				this._rebuildFill(hasPath);
			}
		},
		_rebuildFill : function(items){
			var pathBuilder = this._builder;
			var scope = this;
			var pathId = this.pathId;
			iterate(items, function(item){
				var borders = pathBuilder.matchOpenBorders(item, PathBuilder.HEX_FILL);
				iterate(borders, function(border){
					if( border.id ) scope._createPathImage('images/paths/'+pathId+'_'+border.id, item);
				});
			});
		},
		_border : function(cell){
			var hasPath = this._getCellsWithPaths(cell);
			var pathBuilder = this._builder;
			var scope = this;
			var pathId = this.pathId;
			if( hasPath.length == 0 ){
				cell.path = {id:pathId, solo:true};
			}else{
				cell.path = {id:pathId, solo:false};
				hasPath.push( cell );								
				this._rebuildBorders(hasPath);
			}
		},
		_rebuildBorders : function(items){
			var pathBuilder = this._builder;
			var scope = this;
			var pathId = this.pathId;
			iterate(items, function(item){
				var borders = pathBuilder.buildPath(item);
				iterate(borders, function(border){
					if( border.id ) scope._createPathImage('images/paths/'+pathId+'_'+border.id, item);
				});
			});
		},
		_clean : function(cell){
			var hasPath = this._getCellsWithPaths(cell);
			hasPath.push(cell);
			iterate(hasPath, function(item){
				if( item.images ){
					iterate(item.images, function(i){i.remove();})
				}
			});
		},
		_getCellsWithPaths : function(cell){
			var grid = cell.owner;
			var neighbors = grid.getNeighbors(cell);
			var hasPath = [];
			iterate(neighbors, function(item, index){
				if( !item.path ) return;
				hasPath[index] = item;
			});
			return hasPath;
		}
		
	};
})